<?php

if(!defined('IN_refrigeratory')) {
	exit('Access Denied');
}


$operation = $_REQUEST["operation"];
	$operations = array("get");
if (!in_array($operation, $operations)){
	echo '{"success":false, "info":"request params invalid"}';
	exit;
}

else if($operation == "get"){	
	$uid = $_SESSION["uid"];
	$type  = $_REQUEST["type"];		
	$start = $_REQUEST["start"];
	$limit = $_REQUEST["limit"];
	$sort = ($_REQUEST["sort"] == null)? "" : $_REQUEST["sort"];
	$logfilter = $_POST["filter"];
	$where = "";
if (is_array($logfilter)) {
	for ($i=0;$i<count($logfilter);$i++){
		switch($logfilter[$i]['data']['type']){
			case 'string' : 
			$filed = $logfilter[$i]['field'];
			if($logfilter[$i]['field']=="cname") $filed = "c.name";
			if($logfilter[$i]['field']=="lname") $filed = "l.name";		
			$qs .= " AND ".$filed." LIKE '%".$logfilter[$i]['data']['value']."%'";
			Break;
			case 'list' : 
				if (strstr($logfilter[$i]['data']['value'],',')){
					$fi = explode(',',$logfilter[$i]['data']['value']);
					for ($q=0;$q<count($fi);$q++){
						$fi[$q] = "'".$fi[$q]."'";
					}
					$logfilter[$i]['data']['value'] = implode(',',$fi);
					$qs .= " AND ".$logfilter[$i]['field']." IN (".$logfilter[$i]['data']['value'].")"; 
				}else{
					$qs .= " AND ".$logfilter[$i]['field']." = '".$logfilter[$i]['data']['value']."'"; 
				}
			Break;
			case 'boolean' : $qs .= " AND ".$logfilter[$i]['field']." = ".($logfilter[$i]['data']['value']); Break;
			case 'numeric' : 
				switch ($logfilter[$i]['data']['comparison']) {
					case 'eq' : $qs .= " AND ".$logfilter[$i]['field']." = ".$logfilter[$i]['data']['value']; Break;
					case 'lt' : $qs .= " AND ".$logfilter[$i]['field']." < ".$logfilter[$i]['data']['value']; Break;
					case 'gt' : $qs .= " AND ".$logfilter[$i]['field']." > ".$logfilter[$i]['data']['value']; Break;
				}
			Break;
			case 'date' : 
				switch ($logfilter[$i]['data']['comparison']) {					
					case 'eq' : 
					$qs .= " AND ".$logfilter[$i]['field']." > '".date('Y-m-d',strtotime($logfilter[$i]['data']['value']))." 00:00:00'";
					$qs .= " AND ".$logfilter[$i]['field']." < '".date('Y-m-d',strtotime($logfilter[$i]['data']['value']))." 59:59:59'";
					Break;
					case 'lt' : $qs .= " AND ".$logfilter[$i]['field']." < '".date('Y-m-d',strtotime($logfilter[$i]['data']['value']))." 00:00:00'"; Break;
					case 'gt' : $qs .= " AND ".$logfilter[$i]['field']." > '".date('Y-m-d',strtotime($logfilter[$i]['data']['value']))." 00:00:00'"; Break;
				}
			Break;
		}
	}	
	$where .= $qs;
}
	$qs = "";
	switch($type){
		case 'admin' : 
		break;
		case 'me':
			$sql = "select username from tb_user where uid = '$uid'";		
			$uname = get_value_from_sql($sql,"username");
			$qs .= " AND log.uname LIKE '$uname'";
		break;
		case 'data':
			$sql = "select username from tb_user where uid = '$uid'";		
			$uname = get_value_from_sql($sql,"username");
			$qs .= " AND log.uname LIKE '$uname'";
			$qs .= " AND log.logcat = 2 ";
		break;
		case 'msg':
			$sql = "select username from tb_user where uid = '$uid'";		
			$uname = get_value_from_sql($sql,"username");
			$qs .= " AND log.uname LIKE '$uname'";
			$qs .= " AND log.logcat = 3 ";
		break;
	}
	$where .= $qs;
	/*
	print_r($logfilter);
	echo "<br/>\n";
	 echo "SELECT log.logid , log.uname ,log.time , log.description , l.name lname, c.name cname FROM `tb_log` log,tb_logcat c,tb_loglvl l where log.loglvl = l.loglvl AND log.logcat = c.logcat ".$where." ORDER BY logid DESC";
	exit;
	*/
	
	
	
	/**/
	echo get_page_json_from_sql("SELECT log.logid , log.uname ,log.time , log.description , l.name lname, c.name cname FROM `tb_log` log,tb_logcat c,tb_loglvl l where log.loglvl = l.loglvl AND log.logcat = c.logcat ".$where." ORDER BY logid DESC" , $start , $limit);
	/**/
}

?>